<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ShopMember extends AdminController{
	public $_validation = null;
	
	function __construct(){
		parent::__construct();
		$this->load->model('MShop');
        $this->load->model('MAdmin');
        $this->load->model('MAdminGroup');
        $this->_validation =  array(
            array('field'=>'password', 'label'=>l('password'), 'rules'=>'min_length[3]|max_length[20]')
        ,array('field'=>'role_id', 'label'=>l('role'), 'rules'=>'required|integer')
        ,array('field'=>'name', 'label'=>l('name'), 'rules'=>'required|max_length[20]')
        ,array('field'=>'qq', 'label'=>l('qq'), 'rules'=>'integer|max_length[20]')
        ,array('field'=>'introduction', 'label'=>l('introduction'), 'rules'=>'max_length[255]')
        ,array('field'=>'update_time', 'label'=>'更新时间', 'rules'=>'')
        ,array('field'=>'group_id', 'label'=>'所属管理组', 'rules'=>'required|integer')
        ,array('field'=>'account', 'label'=>'账号', 'rules'=>'required|max_length[20]')
        );
		$allShopAdmins = $this->MAdmin->getAllShopAdmin(1);
        $allAdminGroups = $this->MAdminGroup->getAllGroupsByType(1);

		$this->assign('allShopAdmins', $allShopAdmins);
        $this->assign('allAdminGroups', $allAdminGroups);
	}
	
	function index(){
		//$this->output->enable_profiler(TRUE);
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('a.name', 'like');
		$this->formfilter->addFilter('a.forbidden','where');
        $this->formfilter->addFilter('a.group_id','where');
		
		$limit = $this->pagination($this->MShop->getMemberCount());
		$list = $this->MShop->getMember($limit);
		$this->assign('list', $list);
		$this->layout();
	}

    function add(){
        if(!$this->p->add){
            //权限不足
            show_error(l('user_has_nopower'));
        }
        $this->load->library('form_validation');
        $this->_validation[] =array('field'=>'account', 'label'=>l('account'), 'rules'=>'required|max_length[20]|is_unique[core_admins.account]');
        $this->_validation[0]['rules'] .= "|required";
        $this->form_validation->set_rules($this->_validation);
        $this->load->helper('populate');
        $obj = $this->MAdmin->createVo();
        var_export($this->form_validation->post());
//        exit();
        if($this->form_validation->run()==TRUE){
            $newAdmin = populate($obj, $this->form_validation->post());
            if($this->MAdmin->addAdmin($newAdmin)){
                successAndRedirect(l('add_success'));
            }
            error(l('data_fail'));
        }
        $readonly = '';
        if ($this->_user->role_id == 2){
            $obj->role_id = 4;
            $obj->group_id = $this->_user->group_id;
            $readonly = 'readonly';
        }
        $this->load->model('MRole');
        $roles = $this->MRole->getAll();
        $group = $this->MAdminGroup->getAll();
        $this->assign('admin', $obj);
        $this->assign('roles', object_column($roles,'name','id'));
        $this->assign('groups', object_column($group,'name','id'));
        $this->assign('readonly', $readonly);
        $this->layout();
    }

    function edit($id){
        $this->load->model('MAdminGroup');
        if(!$this->p->edit){
            //权限不足
            show_error(l('user_has_nopower'));
        }
        if(((int)$id) . '' !== $id){
            show_error(l('id_or_updated_not_null'));
        }
        $admin = $this->MAdmin->getById($id);
        if(!$admin/* || $admin->type != GROUP_ADMIN*/){
            show_error('此管理员账号不存在！');
        }
        $this->load->library('form_validation');
        $this->form_validation->set_rules($this->_validation);
        if($this->form_validation->run()==TRUE){
            $this->load->helper('populate');
            $newAdmin = populate($admin, $this->form_validation->post());
            if($this->MAdmin->updateAdmin($newAdmin)){
                successAndRedirect(l('edit_success'));
            }
            //操作冲突
            error(l('data_fail'));
        }
        $this->load->model('MRole');
        $roles = $this->MRole->getAll();
        $group = $this->MAdminGroup->getAll();
        $this->assign('admin', $admin);
        $this->assign('roles', object_column($roles,'name','id'));
        $this->assign('groups', object_column($group,'name','id'));
        $this->layout();
    }

	function delete($id, $update_time){
	    if(!is_numeric($id) || !is_numeric($update_time)){
	        show_error(l('id_not_null'));
	    }
	    if(!$this->MShopmember->delete($id, $update_time)){
	        errorAndRedirect(l('data_fail'));
	    }
	    successAndRedirect(l('delete_success'));
	}
	
	
}